<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">


<head>
  <meta charset="utf-8">
  <title>忘记密码</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport"
    content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="../../layuiadmin/layui/css/layui.css" th:href="@{static/layuiadmin/layui/css/layui.css}"
    media="all">
  <link rel="stylesheet" href="../../layuiadmin/style/admin.css" th:href="@{static/layuiadmin/style/admin.css}"
    media="all">
  <link rel="stylesheet" href="../../layuiadmin/style/login.css" th:href="@{static/layuiadmin/style/login.css}"
    media="all">
  <link rel="stylesheet" href="../../css/mail.css" th:href="@{static/css/mail.css}">
  <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
  <script src="../../js/Particleground.js" th:src="@{static/js/Particleground.js}"></script>
  <script type="text/javascript">
  	var currentemail = "";
  </script>
  <style>
    body {
      height: 100%;
      overflow: hidden;
    }

    canvas {
      z-index: -1;
      position: absolute;
    }
  </style>
  <script>
    $(document).ready(function () {
      //粒子背景特效
      $('body').particleground({
        dotColor: '#5cbdaa',
        lineColor: '#5cbdaa'
      });
    });
  </script>

</head>

<body>

  <div class="layadmin-user-login layadmin-user-display-show" id="LAY-user-login" style="display: none;">
    <div class="layadmin-user-login-main">
      <div class="layadmin-user-login-box layadmin-user-login-header">
        <h2>找回密码</h2>
        <p>请通过你的注册邮箱找回密码</p>
      </div>
      <div class="layadmin-user-login-box layadmin-user-login-body layui-form">
        <div class="layui-form-item">
          <!-- 刘斌 改变固有的图标-->
          <label class="layadmin-user-login-icon layui-icon mailicon icon-tubiao209"
            for="LAY-user-login-cellphone"></label>
          <input type="text" name="email" id="LAY-user-login-cellphone" lay-verify="email" placeholder="邮箱"
            class="layui-input">
        </div>
        <div class="layui-form-item">
          <div class="layui-row">
            <div class="layui-col-xs7">
              <label class="layadmin-user-login-icon layui-icon layui-icon-vercode"
                for="LAY-user-login-vercode"></label>
              <input type="text" name="vercode" id="LAY-user-login-vercode" lay-verify="required" placeholder="验证码"
                class="layui-input">
            </div>
            <div class="layui-col-xs5">
              <div style="margin-left: 10px;">
                <button type="button" class="layui-btn layui-btn-primary layui-btn-fluid"
                  id="getEmailCode">获取验证码</button>
              </div>
            </div>
          </div>
        </div>
        <div class="layui-form-item">
          <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-password"></label>
          <input type="password" name="password" id="LAY-user-login-password" lay-verify="pass" placeholder="新密码"
            class="layui-input">
        </div>
        <div class="layui-form-item">
          <label class="layadmin-user-login-icon layui-icon layui-icon-password" for="LAY-user-login-repass"></label>
          <input type="password" name="repass" id="LAY-user-login-repass" lay-verify="required" placeholder="确认密码"
            class="layui-input">
        </div>
        <div class="layui-form-item">
          <button class="layui-btn layui-btn-fluid" lay-submit lay-filter="LAY-user-reg-submit">确 定</button>
        </div>
        <div class="layui-trans layui-form-item layadmin-user-login-other">
          <label>社交账号注册</label>
          <a href="javascript:;"><i class="layui-icon layui-icon-login-qq"></i></a>
          <a href="javascript:;"><i class="layui-icon layui-icon-login-wechat"></i></a>
          <a href="javascript:;"><i class="layui-icon layui-icon-login-weibo"></i></a>

          <a href="tologin" class="layadmin-user-jump-change layadmin-link layui-hide-xs">用已有帐号登入</a>
          <a href="tologin"
            class="layadmin-user-jump-change layadmin-link layui-hide-sm layui-show-xs-inline-block">登入</a>
        </div>
      </div>
    </div>

    <div class="layui-trans layadmin-user-login-footer">

      <p>© 2018 <a href="#" target="_blank">layui.com</a></p>
      <p>
        <span><a href="#" target="_blank">获取授权</a></span>
        <span><a href="#" target="_blank">在线演示</a></span>
        <span><a href="#" target="_blank">前往官网</a></span>
      </p>
    </div>

  </div>

  <script src="../../layuiadmin/layui/layui.js" th:src="@{static/layuiadmin/layui/layui.js}"></script>
  <script>
    //获取验证码的动画
    function showtTime(nums) {

      if (nums == 0) {
        $("#getEmailCode").attr("disabled", false);
        $("#getEmailCode").html("获取验证码");
        nums = 60;
        return;
      } else {
        $("#getEmailCode").attr("disabled", true);
        $("#getEmailCode").html(nums + 's' + '后重发');
        nums--;
      }
      setTimeout(function () {
        showtTime(nums)
      }, 1000)
    }


    function checkEmail(){
      var mailinput = $("#LAY-user-login-cellphone").val()
      var flag=false;
      $.ajax({
          type:"POST",
          async:false,
          dataType:"text",
          data:{"email":mailinput},
          url:"/checkEmail",
          success:function(data){
            if(data=="1"){
            	layer.msg("邮箱不存在");
              flag=true
              
            }else{
              
            }

          }

        })
        return flag;
    }

    function checkNickName(){
      var nicknameinput=$("#LAY-user-login-nickname").val()
      var flag=false;
      $.ajax({
          type:"POST",
          async:false,
          dataType:"text",
         data:{"nickname":nicknameinput},
          url:"/checkNickname",
          success:function(data){
            if(data=="1"){
              flag=true
              
            }else{
              layer.msg("昵称已存在")
            }

          }

        })
        return flag;
    }
    layui.config({
      base: '/static/layuiadmin/'  //静态资源所在路径
    }).extend({
      index: 'lib/index' //主入口模块
    }).use(['index', 'user'], function () {
      var $ = layui.$
        , setter = layui.setter
        , admin = layui.admin
        , form = layui.form
        , router = layui.router();

      form.render();
      $("#getEmailCode").click(function () {

        var mailverify = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/
        var mailinput = $("#LAY-user-login-cellphone").val()
        if (mailinput == "") {
          layer.msg("请输入邮箱")
          return
        }
        if (!mailverify.test(mailinput)) {
          layer.msg("邮箱格式不正确")
          return
        } else {
        	if(checkEmail()){
        	
        		return 
        	}
          var nums = 60;
          currentemail = mailinput;
          showtTime(nums);
          $.ajax({
            url: "/getverifyemail",
            type: "POST",
            data: { "receiver": mailinput },
            dataType: "text",
            success: function (data) {
              if (data != "1") {
                layer.msg("啊偶，系统出现了一丝丝小问题~")
              }


            },
            error: function () {
              layer.msg("啊偶，系统出现了一丝丝小问题~")
            }
          })





        }
      })

  
      $("#LAY-user-login-cellphone").blur(checkEmail)
      $("#LAY-user-login-nickname").blur(checkNickName)

      //提交
      form.on('submit(LAY-user-reg-submit)', function (obj) {
        var field = obj.field;
        //  console.log(JSON.stringify(field))
        //确认密码
        if (field.password !== field.repass) {
          return layer.msg('两次密码输入不一致');
        }
        if(checkEmail()){
          return false
        }
  

        // //请求接口
        // admin.req({
        //   url: layui.setter.base + 'json/user/reg.js' //实际使用请改成服务端真实接口
        //   , data: field
        //   , done: function (res) {
        //     layer.msg('注册成功', {
        //       offset: '15px'
        //       , icon: 1
        //       , time: 1000
        //     }, function () {
        //       location.hash = '/user/login'; //跳转到登入页
        //     });
        //   }
        // });
        var email = field.email
        var vercode = field.vercode
        var password = field.password
        var repass = field.repass
        var nickname = field.nickname
        var character = field.character
        console.log(vercode);
        if(currentemail != $("#LAY-user-login-cellphone").val()){
        	layui.msg("请勿更改邮箱！");
        	return false;
        }
        $.ajax({
          url: "/setPassword",
          type: "POST",
          dataType: "text",
          data: {
            "username": email,
            "newPassword": password
          },
          success: function (data) {
            if (data == 1) {
              layer.msg("找回成功。正在转向登录界面");
              setTimeout(function(){
                window.location.href="tologin"
              },1000)
              
            } else {
              layer.msg("验证码错误");
            }
          },
          error: function (e) {
            layer.msg("系统异常");
          }
        });


        return false;
      });



    });
  </script>
</body>

</html>